<!--车辆管理页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆管理</span>
    <el-button @click="createVehicle" type="primary" style="float:right;margin-top:13px;">新建车辆</el-button>
  </div>
  <!-- 新增车辆弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;"
  :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆品牌">
            <el-input placeholder="请输入内容" v-model="vehicleSaveForm.brand"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆牌号">
            <el-input placeholder="请输入内容" v-model="vehicleSaveForm.license"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆型号">
            <el-input placeholder="请输入内容" v-model="vehicleSaveForm.model"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆识别码">
            <el-input placeholder="请输入内容" v-model="vehicleSaveForm.code"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆排量">
            <el-select placeholder="请选择" v-model="vehicleSaveForm.displacement">
              <el-option label="1.6" value="1"></el-option>
              <el-option label="2.5" value="2"></el-option>
              <el-option label="4" value="3"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆类型">
            <el-select placeholder="请选择" v-model="vehicleSaveForm.type">
              <el-option label="轿车" value="10"></el-option>
              <el-option label="货车" value="20"></el-option>
              <el-option label="客车" value="30"></el-option>
              <el-option label="挂车" value="40"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车身颜色">
            <el-select placeholder="请选择" v-model="vehicleSaveForm.color">
              <el-option label="黑" value="10"></el-option>
              <el-option label="白" value="20"></el-option>
              <el-option label="蓝" value="30"></el-option>
              <el-option label="灰" value="40"></el-option>
              <el-option label="银" value="50"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="里程数" >
            <el-input placeholder="请输入内容" v-model="vehicleSaveForm.kilometers"> </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD" v-model="vehicleSaveForm.buyTime" @change="validateDate"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上牌时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD" v-model="vehicleSaveForm.regTime" @change="validateDate"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买价格">
            <el-input placeholder="请输入内容" v-model="vehicleSaveForm.price"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电池类型">
            <el-select placeholder="请选择" v-model="vehicleSaveForm.batteryType">
              <el-option label="铅酸蓄电池" value="10"></el-option>
              <el-option label="镍氢电池" value="20"></el-option>
              <el-option label="钠硫电池" value="30"></el-option>
              <el-option label="二次锂电池" value="40"></el-option>
              <el-option label="空气电池" value="50"></el-option>
              <el-option label="三元锂电池" value="60"></el-option>
              <el-option label="碱性燃料电池" value="70"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveVehicle">确定</el-button>
    </template>
  </el-dialog>
  <!-- 车辆搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="车辆品牌">
        <el-input placeholder="请输入内容" style="width:220px;" v-model="searchVehicleForm.brand"></el-input>
      </el-form-item>
      <el-form-item label="车牌号" style="width:290px;">
        <el-input placeholder="请输入内容" style="width:220px;" v-model="searchVehicleForm.license"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadVehicle">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 车辆列表 -->
  <el-card style="margin:20px;" >
    <el-table :data="vehicleArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="brand" label="车辆品牌" align="center"/>
      <el-table-column prop="license" label="车牌号" align="center"/>
      <el-table-column prop="code" label="车辆识别码" align="center"/>
      <el-table-column prop="type" label="车辆类型" align="center"
      :formatter="vehicleTypeFormatter" />
      <el-table-column prop="price" label="购买价格" align="center"/>
      <el-table-column prop="buyTime" label="购买时间" align="center" />
      <el-table-column prop="regTime" label="上牌时间" align="center" />
      <el-table-column prop="batteryType" label="电池类型" align="center"
      :formatter="batteryTypeFormatter" />
      <el-table-column prop="status" label="车辆状态" align="center"
      :formatter="vehicleStatusFormatter"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editVehicle(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteVehicle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";
//控制新增车辆弹窗是否显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('新增车辆');
const searchVehicleForm= ref({
brand:'', license:''
});
const createVehicle=()=>{
  dialogVisible.value=true;
  dialogTitle.value='新增车辆';
}
const saveVehicle = ()=>{
  let data = qs.stringify(vehicleSaveForm.value);
  console.log(data)
   axios.post(BASE_URL+'/v1/vehicle/save',data)
       .then((response)=>{
         if (response.data.code === 2000){
           dialogVisible.value=false;
           vehicleSaveForm.value={};
           loadVehicle();
           ElMessage.success('添加成功');
         }else {
           ElMessage.error(response.data.data);
         }
       })
}



const  vehicleTypeFormatter =(r,c,v,i)=>{
  if (v==="10"){
    v="轿车";
  }else if (v==="20"){
    v="货车";
  }else if (v==="30"){
    v="客车";
  }else if (v==="40"){
    v="挂车";
  }
  return v;
}
const  vehicleStatusFormatter =(r,c,v,i)=> {
  if (v === "1") {
    v = "空闲";
  } else if (v === "2") {
    v = "占用";
  } else if (v === "3") {
    v = "维修";
  }
  return v;
}


const  batteryTypeFormatter =(r,c,v,i)=>{
  if (v==="10"){
    v="铅酸蓄电池";
  }else if (v==="20"){
    v="溴氢电池";
  }else if (v==="30"){
    v="钠硫电池";
  }else if (v==="40"){
    v="二次锂电池";
  }else if (v==="50"){
    v="空气电池";
  }else if (v==="60"){
    v="三元锂电池";
  }else if (v==="70"){
    v="碱性锂电池";
  }
  return v;
}

const vehicleArr = ref([]);

const loadVehicle = ()=>{
    let data = qs.stringify(searchVehicleForm.value);
    console.log(data);
    axios.get(BASE_URL + '/v1/vehicle/select?' + data)
        .then((response) => {
          if (response.data.code === 2000) {
            vehicleArr.value = response.data.data;
            console.log(typeof vehicleArr.value[0].type);
          } else {
            ElMessage.error(response.data.msg);
          }
        })

}

const resetSearch = ()=>{
  searchVehicleForm.value = {};
  loadVehicle();
}


onMounted(()=>{
  loadVehicle();
})


const handleClose = ()=>{
  if (confirm('确认关？')){
    dialogVisible.value=false;
    vehicleSaveForm.value = {};
  }
}

const  editVehicle = (userId)=>{
  dialogTitle.value='编辑车辆';
  dialogVisible.value=true;
  console.log(userId);
  axios.get(BASE_URL+"/v1/vehicle/select?id="+userId)
      .then((response)=>{
        if (response.data.code == 2000){
          vehicleSaveForm.value=response.data.data[0];
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}

const deleteVehicle=(userId)=>{
  if (confirm('确定要删除？')){
    axios.post(BASE_URL+"/v1/vehicle/delete/"+userId)
        .then((response)=>{
          if (response.data.code==2000){
            ElMessage.success("删除成功！");
            loadVehicle();
          }else {
            ElMessage.error(response.data.msg);
          }

        })

  }
}


const vehicleSaveForm = ref({
  brand:'',
  license:'',
  model:'',
  code:'',
  type:'',
  displacement:'',
  kilometers:'',
  color:'',
  price:'',
  buyTime:'',
  regTime:'',
  batteryType:'',
  status:'',
});

const validateDate=()=>{
  const buyTime = vehicleSaveForm.value.buyTime;
  const regTime = vehicleSaveForm.value.regTime;
  console.log(regTime)
  console.log(new Date(regTime));
  if (regTime && buyTime && new Date(regTime) <= new Date(buyTime)){
    ElMessage.error("上牌时间不能早于购买时间");
    vehicleSaveForm.value.regTime='';
  }

}
</script>